package com.extweb4j.core.kit;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.extweb4j.core.bean.SMSException;
import com.extweb4j.core.model.ExtUser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yunpian.sdk.model.ResultDO;
import com.yunpian.sdk.model.SendBatchSmsInfo;
import com.yunpian.sdk.model.SendSingleSmsInfo;
import com.yunpian.sdk.model.TemplateInfo;
import com.yunpian.sdk.model.UserInfo;
import com.yunpian.sdk.service.SmsOperator;
import com.yunpian.sdk.service.TplOperator;
import com.yunpian.sdk.service.UserOperator;
import com.yunpian.sdk.service.YunpianRestClient;

/**
 * 短信发送工具类
 * @author Administrator
 *
 */
public class SMSKit {
	public static YunpianRestClient client = new YunpianRestClient("26e17a737e37837bced8554ee1998fba");
	public static TplOperator tplOperator = client.getTplOperator();
	public static UserOperator userOperator = client.getUserOperator();
	public static SmsOperator smsOperator = client.getSmsOperator();
	public static String signature = "【计算机学院】";
	public static String value = "SUCCESS";
	
	/**
	 * 根据id获取对应的模版
	 * @param tpl_id
	 * @return
	 */
	public static TemplateInfo getTplById(String tpl_id){
		ResultDO<List<TemplateInfo>> resultDO= tplOperator.get();
		for (TemplateInfo info : resultDO.getData()) {
			if(String.valueOf(info.getTpl_id()).equals(tpl_id)){
				return info;
			}
		}
		return null;
	}
	/**
	 * 替换消息模版获得真正的内容
	 * @param tpl_id
	 * @param place
	 * @param time
	 * @return
	 */
	public static String getContent(String tpl_id,String place,String time,String content){
		TemplateInfo info = getTplById(tpl_id);
		String tpl_content =info.getTpl_content();
		tpl_content = tpl_content.replaceAll("#place#", place);
		tpl_content = tpl_content.replaceAll("#time#", time);
		tpl_content = tpl_content.replaceAll("#content#", content);
		return tpl_content;
	}
	/**
	 * 获取用户信息
	 * @return
	 */
	public static UserInfo getUserInfo(){
	 	ResultDO<UserInfo> resultDO= userOperator.get();
	 	return resultDO.getData();
	}
	/**
	 * 根据status获取对应的消息模版
	 * @return
	 */
	public static List<TemplateInfo> getTemplateInfo(String status){
	 	ResultDO<List<TemplateInfo>> resultDO= tplOperator.get();
	 	if(status.equals("ALL")){
	 		return resultDO.getData();
	 	}else{
	 		List<TemplateInfo> list = new ArrayList<TemplateInfo>();
		 	for (TemplateInfo info : resultDO.getData()) {
				if(info.getCheck_status().equals(status)){
					list.add(info);
				}
			}
		 	return list;
	 	}	
	}
	/**
	 * 新增消息模版
	 * @return
	 */
	public static String addTemplate(String content){
		ResultDO<TemplateInfo> result = tplOperator.add(signature+content);
		return getException(result);
	}
	/**
	 * 编辑消息模版
	 * @return
	 */
	public static String editTemplate(String tpl_id,String content){		
		ResultDO<TemplateInfo> result = tplOperator.update(tpl_id,signature+content);
		return getException(result);
	}
	/**
	 * 删除消息模版
	 * @return
	 */
	public static String delTemplate(String tpl_id){
		ResultDO<TemplateInfo> result = tplOperator.del(tpl_id);
		return getException(result);
	}
	/**
	 * 单条发送短信
	 * @param phone
	 * @param content
	 * @return
	 */
	public static String singleSend(String phone,String content){
		ResultDO<SendSingleSmsInfo> result = smsOperator.singleSend(phone,content);
		return getException(result);
	}
	/**
	 * 批量发送短信
	 * @param phone 手机号码
	 * @param content 短息内容
	 * @return
	 */
	public static String batchSend(List<String> phone,String content){
		ResultDO<SendBatchSmsInfo> result = smsOperator.batchSend(phone,content);
		return getException(result);
	}
	/**
	 * 获取异常信息
	 * @param exception
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static String getException(ResultDO result){
		System.out.println(result);
		if(!result.isSuccess()){
			 ObjectMapper mapper = new ObjectMapper();  
		     SMSException se = null;
		     try {
		    	 se = mapper.readValue(result.getE().getMessage(), SMSException.class);
			} catch (Exception e) {
				e.printStackTrace();
			}
		     return se.getDetail();
		}
		
		return value;
	}
	/**
	 * 获得消息用户的邮箱和手机
	 * @param user
	 * @return
	 */
	public static Map<String,List<String>> getPhonesAndEmails(List<ExtUser> user){
		Map<String,List<String>> map = new HashMap<String, List<String>>();
		List<String> phones = new ArrayList<String>();
		List<String> emails = new ArrayList<String>();
		for (ExtUser u : user) {
			emails.add(u.getStr("user_email"));
			phones.add(u.getStr("user_phone"));
		}
		map.put("phones", phones);
		map.put("emails", emails);
		return map;
	}
}
